var aftermath;
(function (aftermath) {
    (function (observability) {
        function isObservable(obj) {
            return ko.isObservable(obj);
        }
        observability.isObservable = isObservable;
        function insert(array, index, items) {
            splice(array, index, 0, items);
        }
        observability.insert = insert;
        function remove(array, index, numToRemove) {
            splice(array, index, numToRemove);
        }
        observability.remove = remove;
        function splice(array, start, howmany, items) {
            array.splice.apply(array, items ? [
                start, 
                howmany
            ].concat(items) : [
                start, 
                howmany
            ]);
        }
        ;
        function getProperty(item, name) {
            return getValue(item[name]);
        }
        observability.getProperty = getProperty;
        function getValue(item) {
            return ko.utils.unwrapObservable(item);
        }
        observability.getValue = getValue;
        function setProperty(item, name, value) {
            return ko.isObservable(item[name]) ? item[name](value) : item[name] = value;
        }
        observability.setProperty = setProperty;
        function asArray(collection) {
            return collection();
        }
        observability.asArray = asArray;
        function map(item, type, dbContext) {
            if(item && item['$values']) {
                item = item['$values'];
            }
            if(aftermath.utils.isArray(item)) {
                var array = aftermath.utils.isValueArray(item) ? item : ko.utils.arrayMap(item, function (value) {
                    return dbContext.map(value, type);
                });
                return ko.observableArray(array);
            }
            if(type in staticTypes) {
                return staticTypes[type](item);
            }
            if(aftermath.utils.isObject(item)) {
                var obj = {
                };
                ko.utils.arrayForEach(aftermath.metadata.getProperties(item, type, true), function (prop) {
                    var value = dbContext.map(item[prop.name], prop.type);
                    obj[prop.name] = ko.isObservable(value) ? value : ko.observable(value);
                });
                return obj;
            }
            return item;
        }
        observability.map = map;
        var staticTypes = {
            'System.Data.Entity.Spatial.DbGeography, EntityFramework': function (dbGeography) {
                var wellKownText = dbGeography['Geography']['WellKnownText'];
                var parts = wellKownText.match(/-?\d+(?:\.\d+)?/g);
                return new google.maps.LatLng(+parts[1], +parts[0]);
            }
        };
    })(aftermath.observability || (aftermath.observability = {}));
    var observability = aftermath.observability;
})(aftermath || (aftermath = {}));
